package com.giaothong;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import com.giaothong.database.SettingDAL;
import com.giaothong.entities.Setting;

public class TabSettingsActivity extends Activity implements OnClickListener{
	
	public static final int SIZE_HISTORY = 10; //su dung cho cac class lien quan

	private SettingDAL settingDAL;
	private Setting setting;

	private Button btn_default;
	private CheckBox check_hienDapAn, checkOffTimer, checkQuestionRandom ;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tab_caidat);
		 
		
		settingDAL = Main.getSettingDAL();
		setting = settingDAL.getSetting();
		unit();
		loadValue(setting);
	}
	private void unit(){
		check_hienDapAn = (CheckBox)findViewById(R.id.settings_checkBox_hienDapAn);
		checkOffTimer = (CheckBox)findViewById(R.id.settings_checkBox_timer);
		checkQuestionRandom = (CheckBox)findViewById(R.id.settings_checkBox_RandomQues);
		btn_default = (Button) findViewById(R.id.settings_btn_default);
		
		btn_default.setOnClickListener(this);
		check_hienDapAn.setOnClickListener(this);
		checkOffTimer.setOnClickListener(this);
		checkQuestionRandom.setOnClickListener(this);
		
	}
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.settings_checkBox_hienDapAn:
			setting = settingDAL.getSetting();
			if(check_hienDapAn.isChecked())
				setting.setHideIsCorrect(true);
			else
				setting.setHideIsCorrect(false);
			
			settingDAL.setSetting(setting);
			break;
		case R.id.settings_checkBox_timer:
			setting = settingDAL.getSetting();
			if(checkOffTimer.isChecked())
				setting.setTimer(true);
			else
				setting.setTimer(false);
			
			settingDAL.setSetting(setting);
			break;
		case R.id.settings_checkBox_RandomQues:
			setting = settingDAL.getSetting();
			if(checkQuestionRandom.isChecked())
				setting.setRandomQuestion(true);
			else
				setting.setRandomQuestion(false);
			
			settingDAL.setSetting(setting);
			break;
		case R.id.settings_btn_default:
			loadValue(settingDAL.reset());
			setting.setHideIsCorrect(true);
			setting.setRandomQuestion(false);
			setting.setTimer(true);
			setting.setHistorySize(SIZE_HISTORY);
			settingDAL.setSetting(setting);
			break;
		}
	}
	private void loadValue(Setting setting) {
		check_hienDapAn.setChecked(setting.getHideIsCorrect());
		checkQuestionRandom.setChecked(setting.getRandomQuestion());
		checkOffTimer.setChecked(setting.getTimer());
	}
	public void restartApp() {
		Intent newIntent = new Intent(this.getApplicationContext(),HomeTabActivity.class);
		finish();
		startActivity(newIntent);
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		this.getParent().onBackPressed();
	}
}